package mk.jug.academy.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import mk.jug.academy.model.Page;
import mk.jug.academy.model.StudentReview;
import mk.jug.academy.model.StudentReviewStatus;

import org.springframework.stereotype.Repository;


/**
 * DAO for the {@link StudentReview} entity.
 *
 * @author Ice.Penov
 */
@Repository
public class StudentReviewDAO extends GenericDAO<StudentReview> {
	
	/**
	 * Gets the {@link StudentReview}s considering the given status,
	 * considering the passed boundaries.
	 */
	public Page<StudentReview> findByStatus(final StudentReviewStatus status, final int start, final int end) {
		return new QueryEMContext<Page<StudentReview>>() {
			@Override @SuppressWarnings("unchecked")
			public Page<StudentReview> query(EntityManager em) {
				Query query = em.createQuery("Select from StudentReview s where s.status = :status order by s.modified desc");
				query.setParameter("status", status);
				query.setFirstResult(start);
				query.setMaxResults(end);
				List<StudentReview> list = query.getResultList();
				query = em.createQuery("Select count(s.id) from StudentReview s where s.status = :status");
				query.setParameter("status", status);
				return new Page<StudentReview>(initialize(list), (Integer) query.getSingleResult());
			}
		}.result();
	}

}